package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageResult;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //分页查询代码
    @GetMapping
    public Result list(String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        // 调用业务层
        PageResult pageResult = empService.query(name, gender, begin, end, pageNum, pageSize);
        return Result.success(pageResult);

    }
  //根据ID查询页面
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
    //新增员工
    @PostMapping
    public Result add(@RequestBody Emp emp){
        empService.add(emp);
        return Result.success();
    }
    //删除员工
    @DeleteMapping({"/{ids}"})
    public Result delete(@PathVariable List<Long> ids){
        empService.delete(ids);
        return Result.success();
    }
}
